import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import Home from '../views/Home/index.vue'
import Cart from '../views/Cart/index.vue'
const CartAddress = () => import("../views/Cart/commpents/CartAddress.vue")
const SubmitBar = () => import("../views/Cart/commpents/SubmitBar.vue")
// import {getReqUserLogin} from '../views/Register'
const Deserve = () => import("../views/Deserve/index.vue")
const Detail = () => import("../views/Detail/index.vue")
const Login = () => import("../views/Login/index.vue")
const LoginPage = () => import("../views/LoginPage/index.vue")
const Register = () => import("../views/Register/index.vue")
const UCenter = () => import("../views/UCenter/index.vue")
const Category = () => import("../views/Category/index.vue")
const Category_list = () => import("../views/Category/Category_list/index.vue")
const Search = () => import("../views/Search/index.vue")

const NotFound = () => import("../views/NotFound/NotFound.vue")
const whiteList = ['/home','/deserve','/detail','/login','/loginpage','/register','/search','/category','/:pathMatch(.*)']
const router = createRouter({
  history:createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/home',
      meta: {
        isShow: true,
        title: '首页'
      },
      scrollBehavior (to, from, savedPosition) {
        return {
          x: 0,
          y: 0
        }
      },
    },
    {
      path: '/home',
      component: Home,
      meta: {
        isShow: true,
        title: '首页'
      },
    },
    {
      path: '/cart',
      name: 'cart',
      component: Cart,
      meta: {
        isShow: true,
        title: '购物车'
      },

    },
    {
      path: '/cart/address',
      name: 'CartAddress',
      meta: {
        title:'商品分类',
        isShow: false,
      },
      component: CartAddress
    },
    {
      path: '/cart/submit',
      name: 'submit',
      component: SubmitBar,
      meta: {
        title:'提交订单',
        isShow: false,
      },
      beforeEnter: (to, from, next) => {
        // reject the navigation
        if (from.name !== 'cart') {
          next({ name: 'cart' })
        }
        next()
      },
    },

    {
      path: '/deserve',
      component: Deserve,
      meta: {
        isShow: true,
        title: '值得买'
      },
    },
    {
      path: '/detail',
      component: Detail,
      meta: {
        isShow: false,
        title: '商品详情'
      },
    },
    {
      path: '/login',
      component: Login,
      meta: {
        isShow: false,
        title: '登录'
      },
    },
    {
      path: '/loginpage',
      component: LoginPage
    },
    {
      path: '/register',
      component: Register,
      meta: {
        isShow: false,
        title: '注册'
      },
    },
    {
      path: '/search',
      component: Search,
      meta: {
        isShow: false,
        title: '搜索'
      },
    },
    {
      path: '/ucenter',
      component: UCenter,
      meta: {
        isShow: true,
        title: '个人中心'
      },
    },
    {
      path: '/category',
      component: Category,
      meta: {
        isShow: true,
        title: '商品分类'
      },
    },
    {
      path: '/category_list',
      component: Category_list,
      meta: {
        title:'商品分类',
        isShow: false,
      },
    },
    {// 404
      path: '/:pathMatch(.*)',
      component: NotFound,
      meta: {
        isShow: false,
        title: '404 NotFound!'
      },
    }
  ]
})

router.afterEach((to, from, next) => {
  document.title = to.meta.title + ' - ' + '网易严选'
})

export default router
  


